package net.pm.core.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import net.pm.common.toolkit.DevConstant;
import net.pm.core.entities.db.Pmdept;
import net.pm.core.entities.db.Pmuser;

import com.google.code.kaptcha.Constants;

import freemarker.ext.beans.BeansWrapper;
import freemarker.template.TemplateHashModel;
import freemarker.template.TemplateModelException;


public abstract class BaseController {
	
	private HttpServletRequest request;
	private HttpSession session;
	
	/**
	 * Access static Method FTL访问静态方法
	 * @param packname
	 * @return
	 * @throws TemplateModelException
	 */
	private final static BeansWrapper wrapper = BeansWrapper.getDefaultInstance();
	private final static TemplateHashModel staticModels = wrapper.getStaticModels();
	protected static TemplateHashModel useStaticPacker(String packname) {
		
		TemplateHashModel fileStatics = null;
		try {
			fileStatics = (TemplateHashModel) staticModels
					.get(packname);
		} catch (TemplateModelException e) {
			e.printStackTrace();
		}
		return fileStatics;
	};
	
	/**
	 * 获取request传递过来的验证码
	 * @return
	 */
	public String obtainCaptchaInRequest(){
		return request.getParameter(DevConstant.REQUEST_KAPTCHA);
	}
	
	/**
	 * 获取session里的验证码
	 * @return
	 */
	public String obtainCaptchaInSession() {
		String kaptchacode = (String) session
				.getAttribute(DevConstant.SESSION_KAPTCHA);
		// 及時清空session中的验证码，避免被重复利用
		this.clearCaptchaCode();
		return kaptchacode;
	}
	
	/**
	 * 清空session里的验证码
	 * 取出，，即清除
	 */
	public void clearCaptchaCode(){
		session.removeAttribute(Constants.KAPTCHA_SESSION_KEY);
	}
	
	/**
	 * 将操作后的提示信息装入session
	 * @param message
	 */
	public void setSessionMessage(String message){
		session.setAttribute(DevConstant.SHOW_MESSAGESS, message);
	}
	
	
	/**
	 * 取出后，随即清空
	 * @return
	 */
	public String obtainSessionMessage(){
		final String smsg = (String) session.getAttribute(DevConstant.SHOW_MESSAGESS);
		setSessionMessage("");
		return smsg;
	}
	
	/**
	 * 获取当前用户
	 * @return
	 */
	public Pmuser obtainCurrentUser(){
		return (Pmuser) session.getAttribute(DevConstant.SESSION_CURRENT_USER);
	}
	
	/**
	 * 获取当前用户登录所在的部门
	 * @return
	 */
	public Pmdept obtainCurrentDept(){
		return (Pmdept) session.getAttribute(DevConstant.SESSION_CURRENT_DEPT);
	}
	

	/**
	 * 如果验证码AOP未通过,一样拦截该方法的AOP是否还停止执行
	 */
	public Boolean aopAbort = false;
	/*

	@SuppressWarnings("unchecked")
	public String obtainPreFootprint() {
		Map<Integer, String> footprint = (Map<Integer, String>) session
				.getAttribute(PMConstant.SESSION_CURRENT_USER_FOOTPRINT);
		
		return footprint.get(footprint.size() - 2);
	}
	@SuppressWarnings("unchecked")
	public void putFootPrint(String url) {
		Map<Integer, String> footprint = (Map<Integer, String>) session
				.getAttribute(PMConstant.SESSION_CURRENT_USER_FOOTPRINT);
		if (PMTool.isEmpty(footprint)){
			footprint = new HashMap<Integer, String>();
			session.setAttribute(PMConstant.SESSION_CURRENT_USER_FOOTPRINT, footprint);
		}
		//加1,,排除为0的key
		footprint.put(footprint.size() + 1, url);
	}
	*//**
	 * 上一个页面
	 * @return
	 *//*
	@RequestMapping(value="pre",method=RequestMethod.GET)
	public String preFootprint(){
		return "redirect:".concat(obtainPreFootprint());
	}*/
	
	public HttpServletRequest obtainRequest() {
		return request;
	}

	public void setRequest(HttpServletRequest request) {
		this.request = request;
	}

	public HttpSession obtainSession() {
		return session;
	}

	public void setSession(HttpSession session) {
		this.session = session;
	} 
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}
